home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / svgakt50.zip / INCLUDE / ZTIMER.H < prev   
C/C++ Source or Header  |  1994-08-23  |  6KB  |  180 lines

  1. /****************************************************************************
  2. *
  3. *                           The Zen Timer Library
  4. *
  5. *                               From the book
  6. *                         "Zen of Assembly Language"
  7. *                            Volume 1, Knowledge
  8. *
  9. *                             by Michael Abrash
  10. *
  11. *                      Modifications by Kendall Bennett
  12. *                   Copyright (C) 1993-4 SciTech Software
  13. *
  14. * Filename:        $RCSfile: ztimer.h $
  15. * Version:        $Revision: 1.11 $
  16. *
  17. * Language:        ANSI C, C++ 2.1
  18. * Environment:    IBM PC (MS DOS)
  19. *
  20. * Description:    Header file for the Zen Timer library. Provides a number
  21. *                of routines to accurately time segments of code. A long
  22. *                period timer is provided to time code that takes up to
  23. *                one hour to execute, with microsecond precision, and an
  24. *                ultra long period timer for timing code that takes up to
  25. *                24 hours to execute (raytracing etc).
  26. *
  27. *                We also provide a set of C++ classes to manipulate
  28. *                the Zen Timers. Note that you can only have one LZTimer
  29. *                running at a time (you can have multiple ULZTimers however),
  30. *                and that the total aggregate time of thr ULZTimer is about
  31. *                65,000 hours, which should suit most timing purposes.
  32. *
  33. *                Note that the precision Zen Timer is no longer supported.
  34. *                This time doesn't work properly in 32 bit protected mode,
  35. *                and we found that in practice it never got used, since we
  36. *                always used the LZTimer instead.
  37. *
  38. * $Id: ztimer.h 1.11 1994/08/22 11:56:19 kjb release $
  39. *
  40. ****************************************************************************/
  41.  
  42. #ifndef    __ZTIMER_H
  43. #define    __ZTIMER_H
  44.  
  45. #ifndef __DEBUG_H
  46. #include "debug.h"
  47. #endif
  48.  
  49. /*-------------------------- Function Prototypes --------------------------*/
  50.  
  51. #define    LZTIMER_RES        1e-6            /* Seconds in a tick            */
  52. #define    ULZTIMER_RES    0.054925        /* Seconds in a tick            */
  53.  
  54. #ifdef    __cplusplus
  55. extern "C" {            /* Use "C" linkage when in C++ mode    */
  56. #endif
  57.  
  58. /* Routine to initialise the library - you MUST call this first! */
  59.  
  60. void    ZTimerInit(void);
  61.  
  62. /* Long period timing routines in LZTIMER.ASM (times up to 1 hour) */
  63.  
  64. void     _cdecl LZTimerOn(void);
  65. ulong    _cdecl LZTimerLap(void);
  66. void     _cdecl LZTimerOff(void);
  67. ulong     _cdecl LZTimerCount(void);
  68.  
  69. /* Ultra long period timing routines in ULZTIMER.C (up to 65,000 hours) */
  70.  
  71. void    ULZTimerOn(void);
  72. ulong    ULZTimerLap(void);
  73. void    ULZTimerOff(void);
  74. ulong    ULZTimerCount(void);
  75. ulong    ULZReadTime(void);
  76. ulong    ULZElapsedTime(ulong start,ulong finish);
  77.  
  78. #ifdef    __cplusplus
  79. }                        /* End of "C" linkage for C++    */
  80. #endif
  81.  
  82. /*--------------------------- Class Definitions ---------------------------*/
  83.  
  84. #ifdef    __cplusplus
  85.  
  86. #ifndef    __IOSTREAM_H
  87. #include <iostream.h>
  88. #endif
  89.  
  90. //---------------------------------------------------------------------------
  91. // Long Period Zen Timer class. This can be used to time code that takes up
  92. // to 1 hour to execute between calls to start() and stop() or lap(). The
  93. // aggregate count can be up to 2^32 - 1 microseconds (about 1 hour
  94. // and 10 mins).
  95. //---------------------------------------------------------------------------
  96.  
  97. class LZTimer {
  98. protected:
  99.     ulong    _count;
  100.     short    _overflow;
  101.  
  102.             // Compute the current count
  103.             void computeTime();
  104.  
  105. public:
  106.             // Constructor
  107.             LZTimer()        { _count = 0; _overflow = false; };
  108.  
  109.             // Method to start the timer
  110.             void start()    { LZTimerOn(); };
  111.  
  112.             // Method to restart the timer
  113.             void restart()    { reset(); start(); };
  114.  
  115.             // Method to return the current count without stop timer
  116.             ulong lap()        { return _count + LZTimerLap(); };
  117.  
  118.             // Method to stop the timer
  119.             void stop()        { LZTimerOff(); computeTime(); };
  120.  
  121.             // Method to return the current count
  122.             ulong count()    { return _count; };
  123.  
  124.             // Method to reset the timer to a zero count
  125.             void reset()    { _count = 0; _overflow = false; };
  126.  
  127.             // Method to determine if overflow occurred
  128.             bool overflow()    { return _overflow; };
  129.  
  130.             // Method to return timer resolution (seconds in a count).
  131.             float resolution()    { return LZTIMER_RES; };
  132.  
  133.             // Method to display the timed count in seconds
  134.     friend    ostream& operator << (ostream& o,LZTimer& timer);
  135.     };
  136.  
  137. //---------------------------------------------------------------------------
  138. // Ultra Long Period Zen Timer class. This can be used to time code that
  139. // takes up 24 hours total to execute between calls to start() and stop().
  140. // The aggregate count can be up to 2^32 - 1 1/18ths of a second, which
  141. // is about 65,000 hours! Should be enough for most applications.
  142. //---------------------------------------------------------------------------
  143.  
  144. class ULZTimer {
  145. protected:
  146.     ulong    _count,_start,_finish;
  147.  
  148. public:
  149.             // Constructor
  150.             ULZTimer()        { _count = 0; };
  151.  
  152.             // Method to start the timer
  153.             void start()    { _start = ULZReadTime(); };
  154.  
  155.             // Method to restart the timer
  156.             void restart()    { reset(); start(); };
  157.  
  158.             // Method to return the current count without stoping timer
  159.             ulong lap()        { return ULZElapsedTime(_start,ULZReadTime()); };
  160.  
  161.             // Method to stop the timer
  162.             void stop();
  163.  
  164.             // Method to return the current count
  165.             ulong count()    { return _count; };
  166.  
  167.             // Method to reset the timer to a zero count
  168.             void reset()    { _count = 0; };
  169.  
  170.             // Method to return timer resolution (seconds in a count).
  171.             float resolution()    { return ULZTIMER_RES; };
  172.  
  173.             // Method to display the timed count in seconds
  174.     friend    ostream& operator << (ostream& o,ULZTimer& timer);
  175.     };
  176.  
  177. #endif
  178.  
  179. #endif
  180.